package cn.edu.sdjzu.xg.eagle.dao;


import cn.edu.sdjzu.xg.eagle.Service.OrganizationDaoInterface;
import cn.edu.sdjzu.xg.eagle.entity.Organization;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

public class OrganizationDao implements OrganizationDaoInterface {

    @Override
    public void save(Connection connection, Organization organization) {
        String sql = "INSERT INTO Organization (name, no, remarks, parent_id) VALUES (?,?,?,?)";
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, organization.getName());
            pstmt.setString(2, organization.getNo());
            pstmt.setString(3, organization.getRemarks());
            if (organization.getParent() != null) {
                pstmt.setLong(4, organization.getParent().getId());
            } else {
                pstmt.setNull(4, java.sql.Types.BIGINT);
            }
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(Connection connection, Organization organization) {
        String sql = "UPDATE Organization SET name = ?, no = ?, remarks = ?, parent_id = ? WHERE id = ?";
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, organization.getName());
            pstmt.setString(2, organization.getNo());
            pstmt.setString(3, organization.getRemarks());
            if (organization.getParent() != null) {
                pstmt.setLong(4, organization.getParent().getId());
            } else {
                pstmt.setNull(4, java.sql.Types.BIGINT);
            }
            pstmt.setLong(5, organization.getId());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Connection connection, Long id) {
        String sql = "DELETE FROM Organization WHERE id = ?";
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setLong(1, id);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public Organization findById(Connection conn, Long id) {
        String sql = "SELECT * FROM Organization WHERE id = ?";
        Organization organization = null;
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setLong(1, id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                organization = new Organization();
                organization.setId(rs.getLong("id"));
                organization.setName(rs.getString("name"));
                organization.setNo(rs.getString("no"));
                organization.setRemarks(rs.getString("remarks"));
                Long parentId = rs.getLong("parent_id");
                if (!rs.wasNull()) {
                    organization.setParent(findById(conn, parentId));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return organization;
    }

    @Override
    public Collection<Organization> findAll(Connection connection) {
        String sql = "SELECT * FROM Organization";
        Collection<Organization> organizations = new ArrayList<>();
        try (PreparedStatement pstmt = connection.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                Organization organization = new Organization();
                organization.setId(rs.getLong("id"));
                organization.setName(rs.getString("name"));
                organization.setNo(rs.getString("no"));
                organization.setRemarks(rs.getString("remarks"));
                Long parentId = rs.getLong("parent_id");
                if (!rs.wasNull()) {
                    organization.setParent(findById(connection, parentId));
                }
                organizations.add(organization);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return organizations;
    }



    @Override
    public Collection<Organization> findAllChildren(Connection connection, Long parentId) {
        String sql = "SELECT * FROM Organization WHERE parent_id = ?";
        Collection<Organization> organizations = new ArrayList<>();
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setLong(1, parentId);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    Organization organization = new Organization();
                    organization.setId(rs.getLong("id"));
                    organization.setName(rs.getString("name"));
                    organization.setNo(rs.getString("no"));
                    organization.setRemarks(rs.getString("remarks"));
                    organization.setChildren(findAllChildren(connection, organization.getId())); // 递归查询子级对象
                    organizations.add(organization);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return organizations;
    }
}
